package ma.controller.panels.auxillary;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;

import ma.model.EBenchmarkInterface;
import ma.model.ManufacturingMethodInterface.SubType;

/**
 * The {@code MMComboBoxListener} creates instances of a
 * {@code ManufacturingMethod} with a ComboBox connected to this listener.
 * The ComboBox carries a client property "method" which tells the
 * controller what type of manufacturing method should be created.
 * @author Oliver Eckstein
 *
 */
public class MMComboBoxListener implements ActionListener {
    
    private EBenchmarkInterface eBenchmark;
    private JComboBox<Integer> cb;
    private int oldCount = 0;
    private CtrInformationPanel infoPanelController;
    
    /**
     * To create a new instance of a {@code ManuMethodsComboBoxListener}.
     * @param eBenchmark the application's model.
     * @param cb the {@code JComboBox}.
     * @param infoPanelController the controller of the information panel.     
     */
    public MMComboBoxListener(
            EBenchmarkInterface eBenchmark,
            JComboBox<Integer> cb,
            CtrInformationPanel infoPanelController) {
        this.eBenchmark = eBenchmark;
        this.cb = cb;
        this.infoPanelController = infoPanelController;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == cb) {
            
            // Create as much manufacturing methods as selected.
            Integer count = (Integer) cb.getSelectedItem();
            String method = (String) cb.getClientProperty("method");
            SubType type = eBenchmark.stringToType(method);            
            if(count > oldCount) {
                eBenchmark.addManufacturingMethods(count - oldCount, type);
            } else if(count < oldCount) {
                eBenchmark.removeManufacturingMethods(oldCount - count, type);
            } else {
                return;
            }
            
            // Set the right text for the info label.
            if (eBenchmark.getSetCount() == 0) {
                infoPanelController.getView().showChooseMethodsError();
            } else if (eBenchmark.getSetCount() > 0 && oldCount == 0) {
                infoPanelController
                    .getView().showChooseMethodsStandardMessage();
            }            
            oldCount = count;
        }
        
    }
}
